<?php
/**
 * Create by chang
 * Date 2019/12/26
 * Time 12:50
 * 导师大赛
 */
namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\TutorMember;
use app\common\model\TutorSetting;
use app\common\model\TutorTeam;
use think\Db;

class Tutor extends Api {

	/**
	 * 活动信息
	 */
	public function index() {
		$row = Db::name('tutorSetting')
			->where(['status' => TutorSetting::STATUS_enable])
			->find();
		
		$team = Db::name('tutorTeam')
			->where(['user_id' => $this->auth->id, 'status' => TutorTeam::STATUS_NORMAL])
			->find();
		
		$row['team_id'] = $team ? $team['id'] : '';
		$this->success('查询成功', $row);
	}
	
	/**
	 * 创建团队
	 */
	public function createTeam() {
		$userId = $this->auth->id;
		$team = Db::name('tutorTeam')
			->where(['user_id' => $userId, 'status' => TutorTeam::STATUS_NORMAL])
			->find();
		if($team) {
			$this->error('你已经创建过团队了');
		}
		
		//判断销量
		$setting = Db::name('tutorSetting')->where(['status' => TutorSetting::STATUS_enable])->find();
		if(!$setting) {
			$this->error('活动已结束');
		}
		
		$number = Db::name('tutorSaleItem')
			->where(['user_id' => $userId, 'create_time' => ['gt', strtotime(date('Y-m-01'))]])
			->sum('number');
		
		if($number < $setting['head_number']) {
			$this->error('需要购买指定商品数量【'.$setting['head_number'].'】,才能创建团队,是否前往购买商品？', ['goods_id' => $setting['goods_id']]);
		}
		
		$model = new TutorTeam();
		$res = $model->isUpdate(false)->save(['user_id' => $userId]);
		if($res) {
			$this->success('创建成功', ['id' => $model->getLastInsID()]);
		} else {
			$this->error('创建出错，请稍候重试。');
		}
	}
	
	/**
	 * 加入团队
	 */
	public function joinTeam() {
		$userId = $this->auth->id;
		$rid = $this->request->post('rid');
		$team = Db::name('tutorTeam')
			->where(['user_id' => $rid, 'status' => TutorTeam::STATUS_NORMAL])
			->find();
		if(!$team) {
			$this->error('团队不存在');
		}
		
		$members = Db::name('tutorMember')->alias('m')
			->where(['m.user_id' => $userId, 'm.status' => TutorMember::STATUS_NORMAL])
			->find();
		
		if($members) {
			$this->error($members['team_id'] == $team['id'] ? '你已加入该团队' : '你已加入别的团队');
		}
		
		$model = new TutorMember();
		$res = $model->isUpdate(false)
			->save([
				'team_id' => $team['id'],
				'user_id' => $userId
			]);
		if($res) {
			$this->success('加入成功');
		} else {
			$this->error('加入出错，请稍候重试');
		}
	}
	
	/**
	 * 团队详情
	 */
	public function teamDetail() {
		$userId = $this->auth->id;
		$team = Db::name('tutorTeam')
			->where(['user_id' => $userId, 'status' => TutorTeam::STATUS_NORMAL])
			->find();
		if(!$team) {
			$this->error('你还没有创建团队');
		}
		
		$members = Db::name('tutorMember')->alias('m')
			->join('user u', 'm.user_id = u.id', 'left')
			->where(['m.team_id' => $team['id'], 'm.status' => TutorMember::STATUS_NORMAL])
			->column('m.user_id, u.nickname, u.avatar', 'u.id');
		
		if($members) {
			$numbers = Db::name('tutorSaleItem')
				->where(['user_id' => ['in', array_keys($members)], 'create_time' => ['gt', strtotime(date('Y-m-01'))]])
				->group('user_id')
				->column('sum(number) as number', 'user_id');
			
			foreach ($members as &$member) {
				$member['number'] = $numbers[$member['user_id']] ??0;
			}
		}
		
		$this->success('查询成功', array_values($members));
	}
}
